﻿using System;
using System.Windows.Media.Imaging;
using Groovy_IM.Protocols.AIM;
using Groovy_IM.Protocols.MSN;
using Groovy_IM.Protocols.MySpace;
using Groovy_IM.Protocols.Yahoo;
using System.Text;
using System.Threading;

namespace Groovy_IM.Protocols
{
    public class Protocol
    {
        public delegate void BuddyChatUpdate2(int Account, string Chat, int Exchange, int Type, string Username, bool EnteredRoom);
        public delegate void BuddyGroup2(int Account, string GroupName, int GroupID);
        public delegate void BuddyItem2(int Account, string BuddyName, int GroupID, int BuddyID);
        public delegate void BuddyStatusUpdate2(int Account, string BuddyName, int Status, string StatusMessage);
        public delegate void IncomingChatInvite2(int Account, string Screenname, string InviteMessage, string Chat, string Exchange, string Type, string Instance, string Details);
        public delegate void IncomingChatMessage2(int Account, string Screenname, string Message);
        public delegate void IncomingICBM2(int Account, string Screenname, string Message);
        public delegate void IncomingUserIcon2(int Account, string Screenname, BitmapImage Avatar);
        public delegate void IncomingUserInformation2(int Account, string Screenname, string Profile, int Status);
        public delegate void IncomingWarn2(int Account, string Screenname, int WarningLevel);
        public delegate void JoinChatError2(int Account, string Chat, string Error);
        public delegate void JoinedChat2(int Account, string Chat, int Exchange, int Type);
        public delegate void LoggedIn2(int Account);
        public delegate void LoggedOut2(int Account);
        public delegate void LoggingIn2(int Account, double CurrentStatus);
        public delegate void LoginError2(int Account, string Error);
        public delegate void MigrationCompleted2(int Account, bool Canceled);
        public delegate void MigrationStarted2(int Account);

        public event BuddyChatUpdate2 BuddyChatUpdate;
        public event BuddyGroup2 BuddyGroup;
        public event BuddyItem2 BuddyItem;
        public event BuddyStatusUpdate2 BuddyStatusUpdate;
        public event IncomingChatInvite2 IncomingChatInvite;
        public event IncomingChatMessage2 IncomingChatMessage;
        public event IncomingICBM2 IncomingICBM;
        public event IncomingUserIcon2 IncomingUserIcon;
        public event IncomingUserInformation2 IncomingUserInformation;
        public event IncomingWarn2 IncomingWarn;
        public event JoinChatError2 JoinChatError;
        public event JoinedChat2 JoinedChat;
        public event LoggedIn2 LoggedIn;
        public event LoggedOut2 LoggedOut;
        public event LoggingIn2 LoggingIn;
        public event LoginError2 LoginError;
        public event MigrationCompleted2 MigrationCompleted;
        public event MigrationStarted2 MigrationStarted;

        private int UserAccount;

        private int ProtoType;

        private OSCAR m_AIM;

        private Live m_Live;

        private Myspace m_Myspace;

        private Yah m_Yahoo;

        public void AddBuddy(int GroupID, string Buddy)
        {
            
        }

        public void AddGroup(string Group)
        {

        }

        public void BlockBuddy(string Buddy)
        {

        }

        public void GrabUserInfo(string Buddy)
        {

        }

        public void InviteFriend(int ChatID, string Friend, string Message)
        {

        }

        public void JoinChat(string Chat, string Exchange, string Type, string Instance, string Detail)
        {
            
        }

        public void RemoveBuddy(int BuddyID, int GroupID)
        {

        }

        public void RemoveGroup(int GroupID)
        {

        }

        public void SendChatMessage(int ChatID, string Message)
        {

        }

        public void SendIM(int MessageID, string Message)
        {

        }

        public void SetStatus(string Status)
        {
            switch (ProtoType)
            {
                case 0:
                    m_AIM.SetStatus(Status);
                    break;
            }
        }

        public void SignOn(int Account)
        {
            UserAccount = Account;
            
            ProtoType = App.Accounts.Items[Account].Protocol;

            string Screenname = App.Accounts.Items[Account].Username;

            string Password = App.Accounts.Items[Account].Password;

            switch (ProtoType)
            {
                case 0:                    
                    m_AIM = new OSCAR();

                    m_AIM.BuddyChatUpdate += new OSCAR.BuddyChatUpdate2(BuddyChatUpdates);

                    m_AIM.BuddyGroup += new OSCAR.BuddyGroup2(BuddyGroups);

                    m_AIM.BuddyItem += new OSCAR.BuddyItem2(BuddyItems);

                    m_AIM.BuddyStatusUpdate += new OSCAR.BuddyStatusUpdate2(BuddyStatusUpdates);

                    m_AIM.IncomingChatInvite += new OSCAR.IncomingChatInvite2(IncomingChatInvites);

                    m_AIM.IncomingChatMessage += new OSCAR.IncomingChatMessage2(IncomingChatMessages);

                    m_AIM.IncomingICBM += new OSCAR.IncomingICBM2(IncomingICBMs);

                    m_AIM.IncomingUserIcon += new OSCAR.IncomingUserIcon2(IncomingUserIcons);

                    m_AIM.IncomingUserInformation += new OSCAR.IncomingUserInformation2(IncomingUserInformations);

                    m_AIM.IncomingWarn += new OSCAR.IncomingWarn2(IncomingWarns);

                    m_AIM.JoinChatError += new OSCAR.JoinChatError2(JoinChatErrors);

                    m_AIM.JoinedChat += new OSCAR.JoinedChat2(JoinedChats);

                    m_AIM.LoggedIn += new OSCAR.LoggedIn2(LoggedIns);

                    m_AIM.LoggedOut += new OSCAR.LoggedOut2(LoggedOuts);

                    m_AIM.LoggingIn += new OSCAR.LoggingIn2(LoggingIns);

                    m_AIM.LoginError += new OSCAR.LoginError2(LoginErrors);

                    m_AIM.MigrationCompleted += new OSCAR.MigrationCompleted2(MigrationCompleteds);

                    m_AIM.MigrationStarted += new OSCAR.MigrationStarted2(MigrationStarteds);

                    m_AIM.Login(App.Accounts.Items[Account].Username, App.Accounts.Items[Account].Password, App.Accounts.Items[Account].Server, App.Accounts.Items[Account].Port, App.Accounts.Items[Account].MobileLogin, App.Accounts.Items[Account].InvisibileLogin, "On Windows Phone 7...");
                    break;
                case 1:
                    m_Live = new Live();
                    break;
                case 2:
                    m_Yahoo = new Yah();
                    break;
                case 3:
                    m_Myspace = new Myspace();
                    break;
            }
        }

        void IncomingWarns(string Screenname, int WarningLevel)
        {
            if (IncomingWarn != null)
                IncomingWarn(UserAccount, Screenname, WarningLevel);
        }

        void MigrationStarteds()
        {
            if (MigrationStarted != null)
                MigrationStarted(UserAccount);
        }

        void MigrationCompleteds(bool Canceled)
        {
            if (MigrationCompleted != null)
                MigrationCompleted(UserAccount, Canceled);
        }

        void LoginErrors(string Error)
        {
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (LoginError != null)
                    LoginError(UserAccount, Error);
            });
        }

        void LoggingIns(double CurrentStatus)
        {
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (LoggingIn != null)
                    LoggingIn(UserAccount, CurrentStatus);
            });
        }

        void LoggedOuts()
        {
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (LoggedOut != null)
                    LoggedOut(UserAccount);
            });
        }

        void LoggedIns()
        {
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (LoggedIn != null)
                    LoggedIn(UserAccount);
            });
        }

        void JoinedChats(string Chat, int Exchange, int Type)
        {
            throw new NotImplementedException();
        }

        void JoinChatErrors(string Chat, string Error)
        {
            throw new NotImplementedException();
        }

        void IncomingUserInformations(string Screenname, string Profile, int Status)
        {
            throw new NotImplementedException();
        }

        void IncomingUserIcons(string Screenname, BitmapImage Avatar)
        {
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                IncomingUserIcon(UserAccount, Screenname, Avatar);
            });
        }

        void IncomingICBMs(string Screenname, string Message)
        {
            throw new NotImplementedException();
        }

        void IncomingChatMessages(string Screenname, string Message)
        {
            throw new NotImplementedException();
        }

        void IncomingChatInvites(string Screenname, string InviteMessage, string Chat, string Exchange, string Type, string Instance, string Details)
        {
            throw new NotImplementedException();
        }

        void BuddyStatusUpdates(string BuddyName, int Status, string StatusMessage)
        {
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (BuddyStatusUpdate != null)
                    BuddyStatusUpdate(UserAccount, BuddyName, Status, StatusMessage);
            });
        }

        void BuddyItems(string BuddyName, int GroupID, int BuddyID)
        {
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (BuddyItem != null)
                    BuddyItem(UserAccount, BuddyName, GroupID, BuddyID); 
            });
        }

        void BuddyGroups(string GroupName, int GroupID)
        {
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (BuddyGroup != null)
                    BuddyGroup(UserAccount, GroupName, GroupID);
            });
        }

        void BuddyChatUpdates(string Chat, int Exchange, int Type, string Username, bool EnteredRoom)
        {
            
        }

        public void SignOff()
        {

        }
    }
}
